﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace CustomsSystem.DAL
{
    /// <summary>
    /// 数据访问类:Bank
    /// </summary>
    public partial class BankService
    {
        public BankService()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cu_Bank");
            strSql.Append(" where ID=" + ID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Cu_Bank  model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.CID != null)
            {
                strSql1.Append("CID,");
                strSql2.Append("" + model.CID + ",");
            }
            if (model.Phone != null)
            {
                strSql1.Append("Phone,");
                strSql2.Append("'" + model.Phone + "',");
            }
            if (model.Fax != null)
            {
                strSql1.Append("Fax,");
                strSql2.Append("'" + model.Fax + "',");
            }
            if (model.Bank != null)
            {
                strSql1.Append("Bank,");
                strSql2.Append("'" + model.Bank + "',");
            }
            if (model.BankNO != null)
            {
                strSql1.Append("BankNO,");
                strSql2.Append("'" + model.BankNO + "',");
            }
            if (model.Remarks != null)
            {
                strSql1.Append("Remarks,");
                strSql2.Append("'" + model.Remarks + "',");
            }
            if (model.Remarks1 != null)
            {
                strSql1.Append("Remarks1,");
                strSql2.Append("'" + model.Remarks1 + "',");
            }
            if (model.Remarks2 != null)
            {
                strSql1.Append("Remarks2,");
                strSql2.Append("'" + model.Remarks2 + "',");
            }
            if (model.Remarks3 != null)
            {
                strSql1.Append("Remarks3,");
                strSql2.Append("'" + model.Remarks3 + "',");
            }
            strSql.Append("insert into Cu_Bank(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Cu_Bank model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Cu_Bank set ");
            if (model.CID != null)
            {
                strSql.Append("CID=" + model.CID + ",");
            }
            if (model.Phone != null)
            {
                strSql.Append("Phone='" + model.Phone + "',");
            }
            else
            {
                strSql.Append("Phone= null ,");
            }
            if (model.Fax != null)
            {
                strSql.Append("Fax='" + model.Fax + "',");
            }
            else
            {
                strSql.Append("Fax= null ,");
            }
            if (model.Bank != null)
            {
                strSql.Append("Bank='" + model.Bank + "',");
            }
            else
            {
                strSql.Append("Bank= null ,");
            }
            if (model.BankNO != null)
            {
                strSql.Append("BankNO='" + model.BankNO + "',");
            }
            else
            {
                strSql.Append("BankNO= null ,");
            }
            if (model.Remarks != null)
            {
                strSql.Append("Remarks='" + model.Remarks + "',");
            }
            else
            {
                strSql.Append("Remarks= null ,");
            }
            if (model.Remarks1 != null)
            {
                strSql.Append("Remarks1='" + model.Remarks1 + "',");
            }
            else
            {
                strSql.Append("Remarks1= null ,");
            }
            if (model.Remarks2 != null)
            {
                strSql.Append("Remarks2='" + model.Remarks2 + "',");
            }
            else
            {
                strSql.Append("Remarks2= null ,");
            }
            if (model.Remarks3 != null)
            {
                strSql.Append("Remarks3='" + model.Remarks3 + "',");
            }
            else
            {
                strSql.Append("Remarks3= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cu_Bank ");
            strSql.Append(" where ID=" + ID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cu_Bank ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Cu_Bank GetModel(int CID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ID,CID,Phone,Fax,Bank,BankNO,Remarks,Remarks1,Remarks2,Remarks3 ");
            strSql.Append(" from Cu_Bank ");
            strSql.Append(" where CID=" + CID + "");
            CustomsSystem.Model.Cu_Bank model = new CustomsSystem.Model.Cu_Bank();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Cu_Bank GetModel(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ID,CID,Phone,Fax,Bank,BankNO,Remarks,Remarks1,Remarks2,Remarks3 ");
            strSql.Append(" from Cu_Bank ");
            if(strWhere !=null &&strWhere !="")
            strSql.Append(" where " + strWhere  + "");
            CustomsSystem.Model.Cu_Bank model = new CustomsSystem.Model.Cu_Bank();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Cu_Bank DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Cu_Bank model = new CustomsSystem.Model.Cu_Bank();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["CID"] != null && row["CID"].ToString() != "")
                {
                    model.CID = int.Parse(row["CID"].ToString());
                }
                if (row["Phone"] != null)
                {
                    model.Phone = row["Phone"].ToString();
                }
                if (row["Fax"] != null)
                {
                    model.Fax = row["Fax"].ToString();
                }
                if (row["Bank"] != null)
                {
                    model.Bank = row["Bank"].ToString();
                }
                if (row["BankNO"] != null)
                {
                    model.BankNO = row["BankNO"].ToString();
                }
                if (row["Remarks"] != null)
                {
                    model.Remarks = row["Remarks"].ToString();
                }
                if (row["Remarks1"] != null)
                {
                    model.Remarks1 = row["Remarks1"].ToString();
                }
                if (row["Remarks2"] != null)
                {
                    model.Remarks2 = row["Remarks2"].ToString();
                }
                if (row["Remarks3"] != null)
                {
                    model.Remarks3 = row["Remarks3"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustomerName  ,Phone ,a .Fax,Bank ,BankNO ,a.Remarks ,a.ID,CID ");
            strSql.Append(" from dbo.Cu_Bank a left join Cu_Customer b on a.CID =b.ID  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,CID,Phone,Fax,Bank,BankNO,Remarks,Remarks1,Remarks2,Remarks3 ");
            strSql.Append(" FROM Cu_Bank ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Cu_Bank ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Cu_Bank T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method
        #region  MethodEx

        #endregion  MethodEx
    }
}

